package ui 
{
	import Base.Loader.SWFLoader;
	import Base.Net.NetSocket;
	
	import Common.GameNetEvent;
	import Common.GameNetManager;
	import Common.RenderManager;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import logic.GameCreatureData;

	/**
	 * ...
	 * @author ...
	 */
	public class RoleInfoUIHandler extends uiBase
	{
		public function RoleInfoUIHandler() 
		{
			path = "roleInfo.swf";
			name = "RoleInfoUI";
		}
		
			
		public override function onInit( loader:SWFLoader ):void
		{
			super.onInit( loader );
			
			uiMc.vitalityUp.addEventListener( MouseEvent.CLICK , onVitUp );
			uiMc.strengthUp.addEventListener( MouseEvent.CLICK , onStrUp );
			uiMc.toughnessUp.addEventListener( MouseEvent.CLICK , onTghUp );
			uiMc.dexterityUp.addEventListener( MouseEvent.CLICK , onDexUp );
			
			uiMc.teamButton.addEventListener( MouseEvent.CLICK , onTeam );
			uiMc.closeButton.addEventListener( MouseEvent.CLICK , onClose );
			
			updateData();
			levelUp();
		}
		
		public override function resize():void
		{
			if ( uiMc )
			{
				uiMc.x = uiMc.px + 15;
				uiMc.y = RenderManager.instance.stageHeight * 0.1 + uiMc.py;
			}
		}
		
		
		public static var instance:RoleInfoUIHandler = null;
		public static function Instance():RoleInfoUIHandler
		{
			if ( instance == null )
			{
				instance = new RoleInfoUIHandler();
			}
			
			return instance;
		}
		
		
		public function levelUp():void
		{
			if ( !uiMc )
			{
				return;
			}
			
			if ( GameManager.instance.MainPlayer.data.LevelUpPoint ) 
			{
				uiMc.levelUp( GameManager.instance.MainPlayer.data.LevelUpPoint );
			}
		}
		
		public function updateData():void
		{
			if ( !uiMc )
			{
				return;
			}
			
			var data:GameCreatureData = GameManager.instance.MainPlayer.data;
			
			uiMc.nameText.text = data.Name;
			uiMc.dpText.text = data.DP.toString();
			uiMc.levelText.text = data.Level.toString();
			uiMc.expText.text = data.EXP.toString();
			uiMc.nextEXPText.text = data.NextEXP.toString();
			
			uiMc.maxHpText.text = data.MAXHP.toString();
			uiMc.hpText.text = data.HP.toString();
			uiMc.mpText.text = data.MP.toString();
			uiMc.attackText.text = data.Attack.toString();
			uiMc.defenceText.text = data.Defence.toString();
			uiMc.quickText.text = data.Quick.toString();
			uiMc.charmText.text = data.Charm.toString();
			
			uiMc.vitalityText.text = data.Vitality.toString();
			uiMc.strengthText.text = data.Strength.toString();
			uiMc.toughnessText.text = data.Toughness.toString();
			uiMc.dexterityText.text = data.Dexterity.toString();
			
			if ( data.LevelUpPoint ) 
			{
				uiMc.levelUpPointText.text = data.LevelUpPoint.toString();
			}
			else
			{
				uiMc.levelUpPointText.text = "";
			}
			
			data = null;
		}
		
		public function onVitUp( e:MouseEvent ):void
		{
			GameManager.instance.MainPlayer.levelUpPoint( GameCreatureData.ATTR_VITALITY );
			updateData();
			
			if ( !GameManager.instance.MainPlayer.getLevelUpPoint() ) 
			{
				uiMc.clear();
			}

			
			var netSocket:NetSocket = GameNetManager.instance.MainNetSocket;

			netSocket.writeShort( GameNetEvent.SEND_LEVLEUP_POINT_SIZE );
			netSocket.writeShort( GameNetEvent.SEND_LEVLEUP_POINT_ID );
			
			netSocket.writeInt( GameCreatureData.ATTR_VITALITY );
			
			netSocket.Send();
		}
		public function onStrUp( e:MouseEvent ):void
		{
			GameManager.instance.MainPlayer.levelUpPoint( GameCreatureData.ATTR_STRENGTH );
			updateData();			
			
			if ( !GameManager.instance.MainPlayer.getLevelUpPoint() ) 
			{
				uiMc.clear();
			}
			
			
			var netSocket:NetSocket = GameNetManager.instance.MainNetSocket;

			netSocket.writeShort( GameNetEvent.SEND_LEVLEUP_POINT_SIZE );
			netSocket.writeShort( GameNetEvent.SEND_LEVLEUP_POINT_ID );
			
			netSocket.writeInt( GameCreatureData.ATTR_STRENGTH  );
			
			netSocket.Send();
		}
		public function onTghUp( e:MouseEvent ):void
		{
			GameManager.instance.MainPlayer.levelUpPoint( GameCreatureData.ATTR_TOUGHNESS );
			updateData();	
			
			if ( !GameManager.instance.MainPlayer.getLevelUpPoint() ) 
			{
				uiMc.clear();
			}
			
			
			var netSocket:NetSocket = GameNetManager.instance.MainNetSocket;

			netSocket.writeShort( GameNetEvent.SEND_LEVLEUP_POINT_SIZE );
			netSocket.writeShort( GameNetEvent.SEND_LEVLEUP_POINT_ID );
			
			netSocket.writeInt( GameCreatureData.ATTR_TOUGHNESS  );
			
			netSocket.Send();
		}
		public function onDexUp( e:MouseEvent ):void
		{
			GameManager.instance.MainPlayer.levelUpPoint( GameCreatureData.ATTR_DEXTERITY );
			updateData();	
			
			if ( !GameManager.instance.MainPlayer.getLevelUpPoint() ) 
			{
				uiMc.clear();
			}
			
			
			var netSocket:NetSocket = GameNetManager.instance.MainNetSocket;

			netSocket.writeShort( GameNetEvent.SEND_LEVLEUP_POINT_SIZE );
			netSocket.writeShort( GameNetEvent.SEND_LEVLEUP_POINT_ID );
			
			netSocket.writeInt( GameCreatureData.ATTR_DEXTERITY );
			
			netSocket.Send();
		}
		
		public function onTeam( e:MouseEvent ):void
		{
			visible( false );
			TeamUIHandler.instance.visible( true );
		}
		
		public function onClose( e:MouseEvent ):void
		{
			visible( false );
		}
		
		
	}

}